package com.naiterui.common.jinshuju.callback.router;

import java.rmi.server.UID;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.naiterui.common.jinshuju.bean.JinshujuCallback;
import com.naiterui.common.jinshuju.bean.JinshujuInfo;
import com.naiterui.common.jinshuju.callback.handler.CallbackHandler;
import com.naiterui.common.jinshuju.exception.JinshujuException;
import com.naiterui.common.jinshuju.service.JinshujuService;

import lombok.Data;

@Data
public class CallbackRouterRule {

    private final CallbackRouter routerBuilder;

    private boolean async = true;

    private String uid;
    private String bizId;
    private String formId;

    private boolean reEnter = false;

    private List<CallbackHandler> handlers = new ArrayList<>();

    public CallbackRouterRule(CallbackRouter routerBuilder) {
        this.routerBuilder = routerBuilder;
    }

    /**
     * 设置是否异步执行，默认是true
     */
    public CallbackRouterRule async(boolean async) {
        this.async = async;
        return this;
    }

    /**
     * uid匹配
     */
    public CallbackRouterRule uid(String uid) {
        this.uid = uid;
        return this;
    }

    /**
     * bizId匹配
     */
    public CallbackRouterRule bizId(String bizId) {
        this.bizId = bizId;
        return this;
    }

    /**
     * formId匹配
     */
    public CallbackRouterRule formId(String formId) {
        this.formId = formId;
        return this;
    }

    public CallbackRouterRule handler(CallbackHandler handler) {
        return this.handler(handler, (CallbackHandler[]) null);
    }

    public CallbackRouterRule handler(CallbackHandler handler, CallbackHandler... otherHandlers) {
        this.handlers.add(handler);
        if (otherHandlers != null && otherHandlers.length > 0) {
            for (CallbackHandler i : otherHandlers) {
                this.handlers.add(i);
            }
        }
        return this;
    }

    public CallbackRouter end() {
        this.routerBuilder.getRules().add(this);
        return this.routerBuilder;
    }

    public CallbackRouter next() {
        this.reEnter = true;
        return this.end();
    }

    protected boolean test(JinshujuCallback jinshujuCallback) {
        return (this.uid == null || this.uid.equalsIgnoreCase(jinshujuCallback.getUid())) &&
                (this.bizId == null || this.bizId.equalsIgnoreCase(jinshujuCallback.getBizId())) &&
                (this.formId == null || this.formId.equalsIgnoreCase(jinshujuCallback.getFormId()));
    }

    /**
     * 处理金数据过来的消息
     */
    protected JinshujuInfo service(JinshujuCallback jinshujuCallback,
                                   Map<String, Object> context,
                                   JinshujuService jinshujuService) {

        if (context == null) {
            context = new HashMap<>();
        }

        try {

            // 交给handler处理
            JinshujuInfo res = null;
            for (CallbackHandler handler : this.handlers) {
                // 返回最后handler的结果
                if (handler == null) {
                    continue;
                }
                res = handler.handle(jinshujuCallback, context, jinshujuService);
            }
            return res;
        } catch (JinshujuException e) {
            //TODO 2019/10/21 3:16 下午 高亮亮
            e.printStackTrace();
        }
        return null;

    }
}
